<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="./widget/base"></div>
</head>
<body>

<div id="userRoleToolbar">
    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/USER_ROLE/PAGE_ALL/USER_ROLE_ADD_DIALOG'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-plus" plain="true"
       onclick="userRoleAddDialog();">授权用户角色</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/USER_ROLE/PAGE_ALL/USER_ROLE_DELETE_ALL'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-close" plain="true"
       onclick="userRoleDeleteAll();">撤销授权用户角色</a>
</div>
<table id="userRole"></table>

<script type="text/javascript">
    var user = $('#user').datagrid('getChecked');
    $(function () {
        if (user) {
            var columns = [
                {field: 'checkbox', checkbox: true},
                {field: 'id', title: 'ID'},
                {field: 'name', title: '角色名'},
                {field: 'gmtModified', title: '更新时间'},
                {field: 'gmtCreated', title: '创建时间'}
            ];
            $('#userRole').datagrid(expressui.grid.create, {
                columns: [columns],
                url: Url.User.Api.ROLE + '/list_all_by_user_id/' + user[0].id
            });
        }
    });

    function userRoleAddDialog() {
        $('#userRoleAddDialog').dialog(expressui.dialog.create, {
            title: '授权用户角色',
            grid: {type: 'datagrid', selector: '#user'},
            href: Url.User.Page.USER_ROLE + '/add_dialog',
            save: {
                url: Url.User.Api.USER_ROLE + '/add_one',
                data: {userId: '{id}'},
                method: 'post'
            },
            buttons: [{
                text: '保存',
                iconCls: 'fa fa-save',
                handler: expressui.dialog.add,
                reload: [{type: 'datagrid', selector: '#userRole'}],
                success: '授权成功'
            }, {
                text: '关闭',
                iconCls: 'fa fa-close',
                handler: expressui.dialog.close
            }]
        });
    }

    function userRoleDeleteAll() {
        var checked = $('#userRole').datagrid('getChecked');
        var roleIdList = [];
        if (checked) {
            for (var i = 0; i < checked.length; i++) {
                roleIdList.push(checked[i][$('#userRole').datagrid('options').idField]);
            }
        }

        $('#userRole').datagrid(expressui.grid.postCheckedLessOneOrShowAlert, {
            url: Url.User.Api.USER_ROLE + '/delete_all_by_user_id_and_role_id_list',
            data: {
                userId: user[0].id,
                roleIdList: roleIdList
            },
            method: 'post',
            confirmMessage: '确定要撤销授权勾选项吗',
            success: '撤销授权成功', reload: {type: 'datagrid', selector: '#userRole'}
        });
    }
</script>
</body>
</html>